package com.hxtec.polaris.commons.thread;

import com.hxtec.polaris.commons.api.vo.Result;
import com.hxtec.polaris.commons.constant.GlobalVar;
import com.hxtec.polaris.commons.constant.Log;
import com.hxtec.polaris.commons.constant.Msg;
import com.hxtec.polaris.commons.exception.MyException;
import com.hxtec.polaris.entity.ShopHistory;
import com.hxtec.polaris.mapper.ShopHistoryMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import java.text.MessageFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;

/**
 * @Describtion 历史浏览记录表
 * @Author yonyong
 * @Date 2019/11/25 15:55
 * @Version 1.0.0
 **/
@Transactional
public class HistoryRunable implements Runnable {

    private final static Logger logger = LoggerFactory.getLogger(BuildPathRunable.class);
    private ServletContext context = null;
    private final CountDownLatch doneSignal;
    private ShopHistory history;

    private ShopHistoryMapper shopHistoryMapper;

    public HistoryRunable(ServletContext context,CountDownLatch doneSignal,ShopHistory history){
        this.context = context;
        this.doneSignal = doneSignal;
        this.history = history;
    }

    @Override
    public void run() {
        initBean(context);
        history.setTime(new Date());
        try{
            synchronized (doneSignal){
                doneSignal.countDown();
                //执行新增，判断库中是否已存在当前用户对应商品的记录
                doInsert();
                //如果历史记录超过30，删除该用户的30条之前的历史记录
                doDelete();
            }
            doneSignal.await();
        }catch (Exception e){
            throw new MyException(Result.error(Msg.COMMON_FAIL),MessageFormat.format(Log.PATTERN_HISTORY,history.toString()));
        }
    }

    /**
     * 获取bean
     * @param context
     */
    private void initBean(ServletContext context){
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        this.shopHistoryMapper = webApplicationContext.getBean(ShopHistoryMapper.class);
    }

    /**
     * 执行insert
     */
    private void doInsert(){
        int count = shopHistoryMapper.getCountBySkuAndUserName(history.getSku(),history.getUsername());
        if (count > 0){
            //如果数据库中对应用户已经有该条记录，将该浏览记录的时间和图片更新
            shopHistoryMapper.updateRows(history);
        }else {
            //如果数据库中对应用户没有该浏览记录，执行新增
            shopHistoryMapper.insert(history);
        }
    }

    /**
     *  如果历史记录超过30，删除该用户的30条之前的历史记录
     */
    private void doDelete(){
        int count = shopHistoryMapper.getCountByUsername(history.getUsername());
        if (count > GlobalVar.HISTORY_ROWS){
            //如果历史记录超过30，删除该用户的30条之前的历史记录
            shopHistoryMapper.deleteOverRowsByUsername(GlobalVar.HISTORY_ROWS,history.getUsername());
        }
    }
}
